package com.xh.audit.service.impl;


import cn.hutool.json.JSONObject;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xh.audit.api.forest.wps.WpsInterface;
import com.xh.audit.api.forest.wps.req.WpsTemplateFileReq;
import com.xh.audit.entity.auditdocument.AuditDocumentTemplate;
import com.xh.audit.mapper.AuditDocumentTemplateMapper;
import com.xh.audit.service.AuditDocumentTemplateService;
import com.xh.common.Err;
import com.xh.common.ExceptionEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 审计文书模版 服务实现类
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class AuditDocumentTemplateServiceImp extends ServiceImpl<AuditDocumentTemplateMapper, AuditDocumentTemplate> implements AuditDocumentTemplateService {
    private final WpsInterface wpsInterface;

    @Override
    public boolean documentTemplateSave(AuditDocumentTemplate tosave) {
        String token = "123"; //todo
        if (tosave.getId() == null) {
            //todo
            tosave.setTemplateCode(System.currentTimeMillis() + "");
            tosave.setUploadeUserName(System.currentTimeMillis() + "");
            tosave.setStatus(0);
            tosave.setUploadeUser(123);
            tosave.setUploadeUserName("qwe");

            this.save(tosave);

            MultipartFile file = tosave.getFile();

            saveWpsTemplateFile(null, tosave, file, token);
        } else {

            AuditDocumentTemplate byId = this.getById(tosave.getId());
            if (byId == null) {
                throw Err.BusinessException.builder()
                        .code(ExceptionEnum.DATA_NULL.getCode() + "")
                        .msg(ExceptionEnum.DATA_NULL.getDescribe())
                        .build();
            }
            Integer wpsTemplateFileId = byId.getWpsTemplateFileId();
            MultipartFile file = tosave.getFile();

            saveWpsTemplateFile(wpsTemplateFileId, tosave, file, token);
        }
        return true;
    }

    private void saveWpsTemplateFile(Integer wpsTemplateFileId, AuditDocumentTemplate tosave, MultipartFile file, String token) {

        WpsTemplateFileReq build = WpsTemplateFileReq.builder()
                .id(wpsTemplateFileId)
                .templateFileName(tosave.getTemplateName())
                .category(tosave.getTemplateType() + "")
                .source("audit")
                .build();

        JSONObject jsonObject = wpsInterface.templateFileSave(token, build, file);

        Integer wpsTemplateFileId_u = (Integer) jsonObject.getByPath("data.id");
        tosave.setStatus(1);
        tosave.setWpsTemplateFileId(wpsTemplateFileId_u);
        this.updateById(tosave);
    }

    @Override
    public void removeByIdsWithWps(List<Integer> ids) {
        List<AuditDocumentTemplate> auditDocumentTemplates = this.listByIds(ids);
        this.removeByIds(ids);

        List<Integer> WpsTemplateFileIds = auditDocumentTemplates.stream().map(itm -> itm.getWpsTemplateFileId()).collect(Collectors.toList());

        if (WpsTemplateFileIds.isEmpty()) {
            return;
        }

        JSONObject res = wpsInterface.templateFileDel(
                "324"//todo
                , WpsTemplateFileIds);

        Integer result = (Integer) res.getByPath("result");
        if (result != 0) {
            log.error(" wpsInterface.templateFileDel返回异常:{}", res);
            throw Err.BusinessException.builder()
                    .code(ExceptionEnum.DO_SOMETHING_ERR.getCode() + "")
                    .msg("删除wps文件模板异常")
                    .build();
        }

    }

}
